﻿using System;     
using System.Collections.Generic;     
using System.Linq;     
using System.Text;     
using System.Threading.Tasks;     
using System.Windows;     
using System.Windows.Controls;     
using System.Windows.Data;     
using System.Windows.Documents;     
using System.Windows.Input;     
using System.Windows.Media;     
using System.Windows.Media.Imaging;     
using System.Windows.Navigation;     
using System.Windows.Shapes;
using System.IO.Ports;
using System.Collections.ObjectModel;

namespace WpfApp2
{     
    /// <summary>     
    /// Interaction logic for MainWindow.xaml     
    /// </summary>     
    public partial class MainWindow : Window     
    {     
        SerialPort sp = new SerialPort();     
        public MainWindow()
        {     
            InitializeComponent();
        }     
     
        private void on_Click(object sender, RoutedEventArgs e)
        {     
            sp.Write("1");     
        }     
     
        private void Of_Click(object sender, RoutedEventArgs e)
        {     
            sp.Write("0");     
        }     
     
        private void Connect_Click(object sender, RoutedEventArgs e)
        {     
            try     
            {     
                String portName = comportno.Text;     
                sp.PortName = ComboBox_COM.Text;
                sp.BaudRate = Convert.ToInt32(ComboBox_Bps.Text);
                //(Int32)ComboBox_Bps.SelectedValue;
                sp.Open();     
                status.Text = "Connected";     
            }     
            catch (Exception)     
            {
                Console.WriteLine("IOException source: {0}", e.Source);
                MessageBox.Show("Please give a valid port number or check your connection");     
            }     
        }     
     
        private void Disconnect_Click(object sender, RoutedEventArgs e)
        {     
            try     
            {     
                sp.Close();     
                status.Text = "Disconnected";     
            }     
            catch (Exception)     
            {     
     
                MessageBox.Show("First Connect and then disconnect");     
            }     
        } 
        
        private void Send_Click(object sender, RoutedEventArgs e)
        {
            String st = "a";
            st += ",";
            st += slider1Value.Text;
            st += ",";
            st += "b";
            st += ",";
            st += slider2Value.Text;
            st += ",";
            st += "d";
            st += ",";
            st += slider3Value.Text;
            sp.Write(st);
        }


    }
}   
